package gov.dhs.cis.soa.data;

import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.Constants.EntityType;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


public class RelatedProperties extends AbstractSOAData {
	static final Logger loger = Logger.getLogger(RelatedProperties.class);

	public int id;
	public int refEntityId;
	public String refEntityType;
	public String propName;
	public String propValue;
	public transient String refEntityHashableId;
	
	@Override
	public String toString() {
		String ret = "RelatedProperties [id=" + id + ", " + propName + "=" + propValue + "]";
		return ret;
	}
	
	public void setPropValue(String val) {
		propValue = val;
	}
	
	public void setPropValue(int val) {
		propValue = ""+val;
	}
	
	public static List<RelatedProperties> getNewProcessProperty(AbstractSOAData refEntity, String name, String value) {
		return convertValueToPropertiesList(refEntity, name, value);
	}

	public static List<RelatedProperties> getNewServiceProperty(AbstractSOAData refEntity, String name, String value) {
		return convertValueToPropertiesList(refEntity, name, value);
	}

	public static List<RelatedProperties> getNewInterfacesProperty(AbstractSOAData refEntity, String name, String value) {
		return convertValueToPropertiesList(refEntity, name, value);
	}
	
	private static List<RelatedProperties> convertValueToPropertiesList(AbstractSOAData refEntity, String name, String value) {
		List<RelatedProperties>retList = new ArrayList<RelatedProperties>();
		if(Utils.isEmpty(value)) {
			loger.warn("Given Value is either Null/Empty, no properties can be defined for <" + name + "> for type: " + refEntity.getUMLType() + ", ID:" + refEntity.getId());
			return retList;
		}
		String[] dStrList = value.split("[,&]");
		for(String dItem : dStrList) {
			dItem = dItem.trim();
			RelatedProperties ret = new RelatedProperties();
			ret.id = Utils.getNextId();
			ret.propName = name;
			ret.propValue = dItem;
			ret.refEntityType = refEntity.getUMLType().toString();
			ret.refEntityId = (int)refEntity.getId();
			ret.refEntityHashableId = refEntity.getHashableId();
			retList.add(ret);
		}
		return retList;
	}
	
//	public static String getUMLId(int entityId) {
//		return EntityType.PROP + "_" + entityId;
//	}
	
	@Override
	public long getId() {
		return id;
	}
	@Override
	public String getHashableId() {
		return refEntityType + "_" + refEntityHashableId + "." + propName + ":" + propValue;
	}
	
	@Override
	public EntityType getUMLType() {
		return EntityType.PROP;
	}
}
